package practice1_100;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;

public class Practice297 {
    ArrayList<Integer> list=new ArrayList<>();
    Queue<TreeNode> queue=new LinkedBlockingQueue<>();

    public  ArrayList<Integer> positiveSerialization(TreeNode root){
        if(root!=null) queue.add(root);
        while (!queue.isEmpty()){
            TreeNode head= queue.poll();
            if(head!=null&&head.val!=Integer.MAX_VALUE) list.add(head.val);
            else list.add(null);

            if(head!=null&&head.val!=Integer.MAX_VALUE) {
                queue.add(head.left!=null?head.left:new TreeNode(Integer.MAX_VALUE));
                queue.add(head.right!=null?head.right:new TreeNode(Integer.MAX_VALUE));
            }
        }

        return list;
    }

    public void dp(TreeNode root){
        if (root==null) list.add(null);
        if(root!=null){
            dp(root.left);
            dp(root.left);
        }
    }

    public static void main(String[] args) {
//        System.out.println(new Practice124().positiveSerialization(new TreeNode(1)));
        TreeNode root=new TreeNode(1);
        root.left=new TreeNode(2);
        root.right=new TreeNode(3);
        root.right.left=new TreeNode(4);
        root.right.right=new TreeNode(5);
        ArrayList<Integer> list= new Practice297().positiveSerialization(root);
        System.out.println(list);
    }
}


